package com.leo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.cell.TextFieldTreeTableCell;
import javafx.scene.control.cell.TreeItemPropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @ClassName TreeTableViewFiveDataDemo
 * @Description CheckBoxTreeTableCell, ChoiceBoxTreeTableCell, ComboBoxTreeTableCell, ProgressBarTreeTableCell, TextFieldTreeTableCell
 * @Author wangss
 * @date 2020.06.17 23:23
 * @Version 1.0
 */
public class TreeTableViewFiveDataDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #f4fffe");

        Data data1 = new Data("A", 12, true);
        Data data2 = new Data("B", 11, true);
        Data data3 = new Data("C", 10, true);
        Data data4 = new Data("D", 14, true);

        TreeTableView<Data> treeTableView = new TreeTableView<>();

        TreeItem<Data> root = new TreeItem<>(data1);
        TreeItem<Data> t2 = new TreeItem<>(data2);
        TreeItem<Data> t3 = new TreeItem<>(data3);
        TreeItem<Data> t4 = new TreeItem<>(data4);
        root.getChildren().addAll(t2, t3, t4);
        treeTableView.setRoot(root);

        root.setExpanded(true);

        treeTableView.setEditable(true);
        TreeTableColumn<Data, String> name = new TreeTableColumn<>("姓名");
        name.setCellValueFactory(new TreeItemPropertyValueFactory<>("name"));
        name.setCellFactory(TextFieldTreeTableCell.forTreeTableColumn());

        TreeTableColumn<Data, Integer> age = new TreeTableColumn<>("年龄");
        age.setCellValueFactory(new TreeItemPropertyValueFactory<>("age"));

        TreeTableColumn<Data, Boolean> flag = new TreeTableColumn<>("是否");
        flag.setCellValueFactory(new TreeItemPropertyValueFactory<>("flag"));

        treeTableView.getColumns().addAll(name, age, flag);
        treeTableView.setPrefWidth(300d);
        treeTableView.setPrefHeight(300d);
        treeTableView.setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY);

        Button button = new Button("bu");
        AnchorPane.setTopAnchor(treeTableView, 150d);
        AnchorPane.setLeftAnchor(treeTableView, 150d);
        an.getChildren().addAll(button, treeTableView);

        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setTitle("TreeTableView");
        primaryStage.setHeight(800d);
        primaryStage.setWidth(800d);
        primaryStage.show();

        button.setOnAction(event -> {
            data1.setName("wangss");
        });
    }
}
